package com.jiage.toolutil.utils;

import android.graphics.Color;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.jiage.sp.SpManager;

/**
 * 作者：佳哥.
 * 时间：2017/3/29.
 * 说明：判断edittext是否都不为空
 */

public class SDEditTextAllNullUtil {
    private int DECIMAL_DIGITS = 10;//小数的位数
    private EditText[] mEditTexts;
    private int[] limitDecimalPointDigit;
    private boolean[] booleans;
    private Button mSubmitBt;
    private boolean mIsBg = false;
    private int mFalseBtBg = 0;
    private int mTrueBtBg = 0;
    private TextView[] mTextViews;
    private onNullListener mNullListener;

    /**
     * 限制小数点后几位  要先于addEditText之前调用
     * 和addEditText一一对应
     * @param limitDecimalPointDigit -1  不限制
     *                               0   不保留小数点
     *                               1   保留一位小数点
     * @return
     */
    public SDEditTextAllNullUtil limitDecimalPointDigit(int[] limitDecimalPointDigit) {
        this.limitDecimalPointDigit = limitDecimalPointDigit;
        return this;
    }

    /**
     * 添加EditTextView
     *
     * @param edit
     * @return
     */
    public SDEditTextAllNullUtil addEditText(EditText[] edit) {
        this.mEditTexts = edit;
        booleans = new boolean[mEditTexts.length];
        for (int i = 0; i < mEditTexts.length; i++) {
            if (mEditTexts[i].getText().toString().trim().length() > 0) {
                booleans[i] = true;
            } else {
                booleans[i] = false;
            }
            if (limitDecimalPointDigit != null) {
                DECIMAL_DIGITS = limitDecimalPointDigit[i];
                if (DECIMAL_DIGITS >= 0) {
                    //手动设置其他位数，例如3
                    mEditTexts[i].setFilters(new InputFilter[]{new SDMoneyValueFilterUtil().setDigits(DECIMAL_DIGITS)});
                }
            }
            final int finalI = i;
            mEditTexts[i].addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {

                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (s.toString().trim().length() > 0) {
                        booleans[finalI] = true;
                    } else {
                        booleans[finalI] = false;
                    }
                    inspectBoolean();
                }
            });
        }
        return this;
    }

    /**
     * 是否全部为空
     */
    private void inspectBoolean() {
        boolean istrue = true;
        for (boolean aBoolean : booleans) {
            if (!aBoolean) {
                istrue = false;
                break;
            }
        }
        if (mSubmitBt != null && mFalseBtBg != 0 && mTrueBtBg != 0) {
            if (istrue&&textViewInspectBoolean()) {
                mSubmitBt.setBackgroundResource(mTrueBtBg);
                mSubmitBt.setEnabled(true);
            } else {
                mSubmitBt.setBackgroundResource(mFalseBtBg);
                mSubmitBt.setEnabled(false);
            }
        }
        if (mSubmitBt != null && mIsBg) {
            if (istrue&&textViewInspectBoolean()) {
                mSubmitBt.setBackgroundColor(SpManager.getInstance().getInt("trueBg",Color.GRAY));
                mSubmitBt.setEnabled(true);
            } else {
                mSubmitBt.setBackgroundColor(SpManager.getInstance().getInt("falseBg",Color.WHITE));
                mSubmitBt.setEnabled(false);
            }
        }
        if(mNullListener!=null){
            mNullListener.onNull(istrue&&textViewInspectBoolean());
        }
    }

    /**
     * 判断TextView是否为空
     * @return
     */
    private boolean textViewInspectBoolean(){
        boolean isTrue = true;
        if(mTextViews==null)
            return isTrue;
        for (TextView mTextView : mTextViews) {
            if(mTextView.getText().toString().length() == 0){
                isTrue = false;
                break;
            }
        }
        return isTrue;
    }

    /**
     * 需要改变的按钮
     *
     * @param button
     * @return
     */
    public SDEditTextAllNullUtil setSubmitBt(Button button) {
        this.mSubmitBt = button;
        return this;
    }
    /**
     * 需要改变的按钮
     *
     * @param button   是否自动设置按钮背景
     * @return
     */
    public SDEditTextAllNullUtil setSubmitBt(Button button, boolean isBg) {
        this.mSubmitBt = button;
        this.mIsBg = isBg;
        return this;
    }

    /**
     * 按钮false是的背景
     *
     * @param bg
     * @return
     */
    public SDEditTextAllNullUtil setFalseBtBg(int bg) {
        this.mFalseBtBg = bg;
        return this;
    }

    /**
     * 按钮true是的背景
     *
     * @param bg
     * @return
     */
    public SDEditTextAllNullUtil setTrueBtBg(int bg) {
        this.mTrueBtBg = bg;
        return this;
    }

    /**
     * 添加TextViews
     * @param textViews
     */
    public SDEditTextAllNullUtil addTextView(TextView[] textViews){
        this.mTextViews = textViews;
        return this;
    }

    public SDEditTextAllNullUtil setOnNullListener(onNullListener nullListener){
        this.mNullListener = nullListener;
        return this;
    }

    public interface onNullListener{
        void onNull(boolean isNull);
    }

    public SDEditTextAllNullUtil start() {
        inspectBoolean();
        return this;
    }
}
